package com.mlog.njyjy.ndop.msg.relay.task;

import com.mlog.njyjy.ndop.msg.relay.icallinterface.callBack;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: NMC-DevOps-Platform
 * @description: ${description}
 * @author: socker
 * @create: 2020-07-22 09:27
 */
@Slf4j
public abstract class BaseTask {

    protected static final Map<String, Boolean> TASK_RUNNING_STATUS_MAP = new HashMap<>(32);

    public abstract void startJob();

    boolean useDataGetFrom = false;


    protected void startJobWithCheckStatus(callBack callBack) {
        String className = getClass().getSimpleName();
        boolean isTaskRunning = TASK_RUNNING_STATUS_MAP.getOrDefault(className, false);
        if (isTaskRunning) {
            log.info(className + " task is already running.");
            return;
        }
        log.info(className + " startJob, go -> ");
        try {
            TASK_RUNNING_STATUS_MAP.put(className, true);
            if (useDataGetFrom) {
                try {
                    checkAndDealDataFrom();
                } catch (Exception e) {
                    log.error(className + " checkAndDealDataFrom ERROR", e);
                }
            }
            callBack.execute();
        } finally {
            TASK_RUNNING_STATUS_MAP.put(className, false);
        }
        log.info(className + " startJob, done.");
    }

    public void checkAndDealDataFrom() {

    }

    public void run() {
        doJob();
    }

    protected void doJob() {
        throw new RuntimeException("doFile shoud be implemented in subClass");
    }
}